<template>
  <LoginHeader>联合登录</LoginHeader>
  <div class="loading" v-if="isBind"></div>
  <section class="container" v-else>
    <nav class="tab">
      <a @click="hasAccount=true" :class="{active:hasAccount}" href="javascript:;">
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a @click="hasAccount=false" :class="{active:!hasAccount}" href="javascript:;">
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <CallbackBind :userInfo="userInfo" :unionId="unionId" />
    </div>
    <div class="tab-content" v-else>
      <CallbackPatch :unionId="unionId" />
    </div>
  </section>
  <LoginFooter />
</template>

<script>
import { ref } from 'vue'
import LoginHeader from './components/login-header.vue'
import LoginFooter from './components/login-footer.vue'
import CallbackBind from './components/callback-bind.vue'
import CallbackPatch from './components/callback-patch.vue'
import { userQQLogin } from '@/api/user'
import { Message } from '@/components/library/Message'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'PageCallback',
  components: { LoginHeader, LoginFooter, CallbackBind, CallbackPatch },
  setup () {
    const hasAccount = ref(true)
    // 如果 已绑定true就显示 loading，未绑定false显示表单结构
    const isBind = ref(true)

    // 获取当前登录QQ用户的Access Token以及 OpenID (用户唯一标识)
    const unionId = ref('')
    const store = useStore()
    const router = useRouter()
    if (QC.Login.check()) {
      QC.Login.getMe(async (openId, accessToken) => {
        unionId.value = openId
        // console.log({ openId, accessToken })
        try {
          const { result } = await userQQLogin(openId)
          // 注册成功提示
          Message({ type: 'success', message: 'QQ登录成功' })
          // 存储用户信息
          store.commit('user/setUser', result)
          // QQ授权登录也要同步购物车
          store.dispatch('cart/mergeLocalCart')
          // 跳转页面
          router.push(store.state.user.redirectUrl || '/')
        } catch (error) {
          // console.log('需要绑定')
          // 未绑定显示表单结构
          isBind.value = false
        }
      })
    }

    const userInfo = ref(null)
    // 获取当前登录QQ用户的公开信息
    if (QC.Login.check()) {
      QC.api('get_user_info').success(res => {
        // console.log('获取用户公开信息', res)
        userInfo.value = res.data
      })
    }

    return { hasAccount, userInfo, unionId, isBind }
  }
}
</script>

<style scoped lang='less'>
.loading {
  margin: 25px auto;
  width: 1240px;
  height: 730px;
  background: #fff url(~@/assets/images/load.gif) no-repeat center / 100px 100px;
}
.container {
  padding: 25px 0;
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
